<template>
    <div class="app-container" :gutter="20">
        <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :md="6" :lg="6">
                <el-card class="box-card">
                    <el-row :gutter="10" class="mb8">
                        <div>工资类型</div>
                    </el-row>
                    <el-table
                        :border="Global.tableShowBorder"
                        v-loading="loading"
                        :data="typeList"
                        @row-click="selectRow"
                        highlight-current-row
                    >
                        <el-table-column
                            label="名称"
                            align="center"
                            prop="name"
                        />
                        <el-table-column
                            label="操作"
                            align="center"
                            prop="status"
                            width="120"
                        >
                            <template slot-scope="scope">
                                <el-switch
                                    v-model="scope.row.status"
                                    active-value="0"
                                    inactive-value="1"
                                    @change="changeTypeStatus(scope.row)"
                                ></el-switch>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-card>
            </el-col>

            <el-col :xs="24" :sm="12" :md="18" :lg="18">
                <el-card class="box-card">
                    <el-row :gutter="10" class="mb8">
                        <el-col :span="1.5">
                            <el-button
                                type="primary"
                                plain
                                icon="el-icon-plus"
                                size="mini"
                                @click="handleAdd"
                                v-hasPermi="['hrm:salaryItemConfig:add']"
                            >
                                新增
                            </el-button>
                        </el-col>
                        <right-toolbar
                            :showSearch.sync="showSearch"
                            @queryTable="getList"
                        ></right-toolbar>
                    </el-row>

                    <el-table
                        :border="Global.tableShowBorder"
                        v-loading="loading"
                        :data="salaryItemConfigList"
                    >
                        <el-table-column
                            label="名称"
                            align="center"
                            prop="name"
                            width="180"
                        />
                        <el-table-column
                            label="描述"
                            align="center"
                            prop="remark"
                        />
                        <el-table-column
                            label="加/减"
                            align="center"
                            prop="oper"
                            width="80"
                        >
                            <template slot-scope="scope">
                                <span
                                    v-if="scope.row.oper == '0'"
                                    style="
                                        color: #ff4949;
                                        font-size: 14px;
                                        font-weight: 900;
                                    "
                                    >加</span
                                >
                                <span
                                    v-if="scope.row.oper == '1'"
                                    style="
                                        color: #1ab394;
                                        font-size: 14px;
                                        font-weight: 900;
                                    "
                                    >减</span
                                >
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="是否计税"
                            align="center"
                            prop="ifTax"
                            width="80"
                        >
                            <template slot-scope="scope">
                                <dict-tag
                                    :options="dict.type.sys_yes_no"
                                    :value="scope.row.ifTax"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="操作"
                            align="center"
                            class-name="small-padding fixed-width"
                            width="150"
                        >
                            <template slot-scope="scope">
                                <el-button
                                    size="mini"
                                    v-if="scope.row.isCustom == 'Y'"
                                    type="text"
                                    icon="el-icon-edit"
                                    @click="handleUpdate(scope.row)"
                                    v-hasPermi="['hrm:salaryItemConfig:edit']"
                                >
                                    修改
                                </el-button>
                                <el-button
                                    size="mini"
                                    v-if="scope.row.isCustom == 'Y'"
                                    type="text"
                                    icon="el-icon-delete"
                                    @click="handleDelete(scope.row)"
                                    v-hasPermi="['hrm:salaryItemConfig:remove']"
                                >
                                    删除
                                </el-button>

                                <el-switch
                                    v-model="scope.row.status"
                                    v-if="scope.row.isCustom == 'N'"
                                    active-value="0"
                                    inactive-value="1"
                                    @change="handleStatusChange(scope.row)"
                                ></el-switch>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-card>
            </el-col>
        </el-row>

        <!-- 添加或修改工资项配置对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="500px"
            append-to-body
        >
            <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="120px"
            >
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="加/减" prop="oper">
                    <el-select v-model="form.oper" placeholder="请选择是加/减">
                        <el-option key="0" label="加" value="0"></el-option>
                        <el-option key="1" label="减" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否计税" prop="ifTax">
                    <el-select
                        v-model="form.ifTax"
                        placeholder="请选择是否计税"
                    >
                        <el-option
                            v-for="dict in dict.type.sys_yes_no"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="描述信息" prop="remark">
                    <el-input
                        v-model="form.remark"
                        type="textarea"
                        :rows="5"
                        placeholder="请输入内容"
                    />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listSalaryItemConfig,
    getSalaryItemConfig,
    delSalaryItemConfig,
    addSalaryItemConfig,
    updateSalaryItemConfig,
    updateStatusByType,
} from "@/api/hrm/salaryItemConfig";

export default {
    name: "SalaryItemConfig",
    dicts: ["sys_yes_no"],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 工资项配置表格数据
            salaryItemConfigList: [],
            typeList: [
                { id: "0", name: "基本工资", status: "1" },
                { id: "1", name: "津贴", status: "1" },
                { id: "2", name: "浮动工资", status: "1" },
                { id: "3", name: "奖金", status: "1" },
                { id: "4", name: "提成工资", status: "1" },
                { id: "5", name: "计件工资", status: "1" },
                { id: "6", name: "计时工资", status: "1" },
                { id: "7", name: "工龄/司龄工资", status: "1" },
                { id: "8", name: "职称工资", status: "1" },
                { id: "9", name: "代扣代缴", status: "1" },
                { id: "10", name: "企业代缴", status: "1" },
                { id: "11", name: "税前补发", status: "1" },
                { id: "12", name: "税前补扣", status: "1" },
                { id: "13", name: "税后补发", status: "1" },
                { id: "14", name: "税后补扣", status: "1" },
                { id: "15", name: "特殊计费项", status: "1" },
                { id: "16", name: "加班工资", status: "1" },
                { id: "17", name: "考勤组扣款明细", status: "1" },
                { id: "18", name: "个税抵扣", status: "1" },
                { id: "50", name: "应发工资", status: "1" },
                { id: "51", name: "应税工资", status: "1" },
                { id: "52", name: "个人所得税", status: "1" },
                { id: "53", name: "实发工资", status: "1" },
            ],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                // // pageNum: 1,
                // // pageSize: 20,
                // orderByColumn: "createTime",
                // isAsc: "descending",
                type: null,
                name: null,
                oper: null,
                ifTax: null,
                tenantId: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            type: "",
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询工资项配置列表 */
        getList() {
            this.loading = true;
            this.queryParams.type = this.type;
            listSalaryItemConfig(this.queryParams).then((response) => {
                this.loading = false;
                if (response.data.length == 0) {
                    this.$tab.closeOpenPage({
                        path: "/hrm/config/salaryItemCfg/init",
                    });
                }
                this.salaryItemConfigList = response.data;
                let typeArr = this.typeList;
                this.salaryItemConfigList.forEach((item) => {
                    typeArr.forEach((t) => {
                        console.log(
                            item.type +
                                "," +
                                item.name +
                                "。" +
                                t.id +
                                "," +
                                t.name
                        );
                        if (t.id == item.type) {
                            if (item.status == "0") {
                                t.status = "0";
                            }
                        }
                    });
                });

                this.typeList = typeArr;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                type: null,
                name: null,
                oper: null,
                ifTax: null,
                tenantId: null,
                createBy: null,
                remark: null,
            };
            this.resetForm("form");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            if (!this.type) {
                this.$modal.msgError(
                    "请先左侧工资类型，然后再点击添加按钮进行添加"
                );
                return;
            }
            this.reset();
            this.open = true;
            this.title = "添加工资项配置";
            this.form.type = this.type;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getSalaryItemConfig(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "修改工资项配置";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateSalaryItemConfig(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addSalaryItemConfig(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除工资项配置编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delSalaryItemConfig(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "hrm/salaryItemConfig/export",
                {
                    ...this.queryParams,
                },
                `salaryItemConfig_${new Date().getTime()}.xlsx`
            );
        },

        //类型下的某一行改状态
        handleStatusChange(row) {
            let form = {};
            form.id = row.id;
            form.status = row.status;
            console.log(form);
            updateSalaryItemConfig(form).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
            });
        },

        //整个类型改状态
        changeTypeStatus(row) {
            let form = {};
            form.status = row.status;
            form.type = row.id;
            updateStatusByType(form).then((res) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
            });
        },
        selectRow(row, column) {
            // this.$refs.table.clearSelection();
            // this.$refs.table.setCurrentRow(row);

            this.type = row.id;
            this.getList();
        },
    },
};
</script>
